<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        宿舍管理
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/css/main.css" media="all">
    <script type="text/javascript" src="/lib/loading/okLoading.js"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>宿舍管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <el-form :model="dormitory" :rules="rules" ref="dormitory" label-width="120px" class="demo-ruleForm">
        <el-form-item label="宿舍编号" prop="did">
            <el-input v-model.number="dormitory.did"></el-input>
        </el-form-item>
        </el-form-item>
        <el-form-item label="剩余电费" prop="elcRemain">
            <el-input v-model.number="dormitory.elcRemain"></el-input>
        </el-form-item>
        <!--el-form-item label="供电情况" prop="area">
            <el-input v-model="dormitory.area"></el-input>
        </el-form-item-->
        <el-form-item label="供电情况" prop="elcStatus">
            <el-radio-group v-model="dormitory.elcStatus">
                <el-radio label="是">正常</el-radio>
                <el-radio label="否">异常</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="剩余水费" prop="waterRemain">
            <el-input v-model.number="dormitory.waterRemain"></el-input>
        </el-form-item>
        <el-form-item label="供水情况" prop="waterStatus">
            <el-radio-group v-model="dormitory.waterStatus">
                <el-radio label="是">正常</el-radio>
                <el-radio label="否">异常</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="入住人数" prop="dnum">
            <el-input v-model.number="dormitory.dnum"></el-input>
        </el-form-item>
        <el-form-item label="床位数" prop="bed">
            <el-input v-model.number="dormitory.bed"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="save('dormitory')" style="text-align: center">保存</el-button>
            <el-button @click="clearAll()" style="text-align: center">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script src="/js/vue.js" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script src="/js/index.js"></script>
<!--导入Axios依赖包-->
<script src="/js/axios.js" charset="utf-8"></script>
<script type="text/javascript">
    var app = new Vue({
        el:"#app",
        data:{
            title:"添加宿舍",
            flag:null,
            dormitory:{did:""},
            fileList:[],
            rules: {
                did: [
                    { required: true, message: '请输入宿舍编号', trigger: 'blur' },
                    { type: 'number', message: '宿舍号必须为数字值'},
                    //{ maxlength:"3", message: '长度为3个数字', trigger: 'blur' },
                    { pattern:/^\d{3}$/,message:'长度为3',trigger:'blur'}
                ],

                elcRemain: [
                    { required: true, message: '请输入剩余电费', trigger: 'blur' },
                    { type: 'number', message: '剩余电费必须为数字值'},
                    { pattern:/^\d{1,6}$/,message:'长度为最大为6位数',trigger:'blur'}
                ],
                elcStatus:[
                    { required: true, message: '请选择供电状态', trigger: 'change' }
                ],
                waterRemain: [
                    { required: true, message: '请输入剩余水费', trigger: 'blur' },
                    { type: 'number', message: '剩余水费必须为数字值'},
                    { pattern:/^\d{1,6}$/,message:'长度为最大为6位数',trigger:'blur'}
                ],
                waterStatus:[
                    { required: true, message: '请选择供水状态', trigger: 'change' }
                ],
                dnum: [
                    { required: true, message: '请输入入住人数', trigger: 'blur' },
                    { type: 'number', message: '入住人数必须为数字值'},
                    { pattern:/^\d{1,2}$/,message:'长度为最大为2位数',trigger:'blur'}
                ],
                bed:[
                    { required: true, message: '请输入总床位数', trigger: 'blur' },
                    { type: 'number', message: '总床位数必须为数字值'},
                    { pattern:/^\d{1,2}$/,message:'长度为最大为2位数',trigger:'blur'}
                ]
            }
        },
        methods:{
            findById(){
                axios.get("/dormitory/findById?did="+this.dormitory.did).then((res)=>{
                    this.dormitory = res.data.data;

                });
            },


            save(formName){

                    this.$refs[formName].validate((valid) => {
                        if (!valid) {
                            alert('error submit!!')
                            console.log('error submit!!');
                            return false;
                        } else {


                var method="";
                axios.post("/dormitory/count?did="+this.dormitory.did).then((res)=>{
                    this.flag = res.data;
                    if(this.flag){
                        method = "add"
                    }else{
                        method = "update"
                    }

                    axios.post("/dormitory/"+method,this.dormitory).then((res)=>{
                        if(res.data.flag){
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                        }
                    });
                })
                        }
                    });
            },
            getFlag(){
                axios.post("/dormitory/count?did="+this.dormitory.did).then((res)=>{
                        this.flag = res.data;});
            },
            getQueryString(name){
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return '';
            }
        },
        created:function(){
            layui.use(['element','layer','form'], function(){
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.dormitory.did = this.getQueryString("did");

            //根据小区ID获取小区对象，进行数据回显
            if(this.dormitory.did!=null&&this.dormitory.did!=''){

                this.title="修改宿舍";
                this.findById();
            }
        }
    });
</script>
</body>
</html>